﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Models.Report;
using S4Store.Domain;
using S4Store.Service;
using S4Store.Domain.AfterSaleKpi;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using S4Store.Models;
using S4Store.Models.AfterSaleReport;

namespace S4Store.Controllers
{
    public class AccidentController : ReportBaseController
    {
        public AfterSaleCarKPIService AfterSaleCarKPIService { get; set; }
        public EmployeeService EmployeeService { get; set; }
        //
        // GET: /Accident/

        public ActionResult Index()
        {
            AfterSaleHealthyCarTypeModel model = new AfterSaleHealthyCarTypeModel();
            using (var t = new Transtion())
            {

                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());

                t.Inject(AfterSaleCarKPIService);
                model.应时台次 = (int)((decimal)AfterSaleCarKPIService.GetAfterSaleCarTypeMonthConfigs(Month.Year, Month.Month, Store.Id).Where(o => o.IsConfirmed).Sum(o => o.BYSGCRCTCMB_MonthAacidentCarToFactoryTarget) * 时间进度);

                model.实际台次 = AfterSaleCarKPIService.GetAfterSaleCarTypeMonthFinished(Month.Year, Month.Month, Store.Id).Where(o => o.IsConfirmed).Sum(o => o.SGRCTC_AccidentCarToFactory);

                model.实际产值 = Math.Round(AfterSaleCarKPIService.GetAfterSaleCarTypeMonthFinished(Month.Year, Month.Month, Store.Id).Where(o => o.IsConfirmed).Sum(o => o.SGCCZ_AccidentCarValue), 2, MidpointRounding.AwayFromZero);

                model.应时产值 = Math.Round(AfterSaleCarKPIService.GetAfterSaleCarTypeMonthConfigs(Month.Year, Month.Month, Store.Id).Where(o => o.IsConfirmed).Sum(o => o.BYSGCCZBM_MonthAacidentCarTargetValue) * 时间进度, 2, MidpointRounding.AwayFromZero);

                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "Index";
                model.BarModel.Controller = "Accident";
                //model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='销售台次历史记录' href='" + Url.Action("SaleHistroy") + "' ><span>历史数据查询</span></a>";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                t.Commit();

            }
            return View(model);
        }

        public ActionResult AccidentCars_RCTC()
        {
            AfterSaleHealthyCarBrand_RCTCModel model = new AfterSaleHealthyCarBrand_RCTCModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(AfterSaleCarKPIService);

                model.RCTCList = new List<AfterSaleHealthyCarBrand_RCTCModel.RCTCMdel>();
                var queryMonth = AfterSaleCarKPIService.GetAfterSaleCarTypeMonthConfigs(Month.Year, Month.Month, Store.Id).Where(o => o.IsConfirmed);
                var queryDay = AfterSaleCarKPIService.GetAfterSaleCarTypeMonthFinished(Month.Year, Month.Month, Store.Id).Where(o => o.IsConfirmed);
                var Brandlist = queryMonth.Select(o => o.CarBrand).Distinct();
                foreach (var item in Brandlist)
                {
                    model.RCTCList.Add(new AfterSaleHealthyCarBrand_RCTCModel.RCTCMdel()
                    {
                        车型ID = item.Id,

                        车型名称 = item.Name,

                        月应时台次 = queryMonth.Where(o => o.CarBrandId == item.Id).Sum(o => o.BYSGCRCTCMB_MonthAacidentCarToFactoryTarget),

                        应时台次 = (int)Math.Round((decimal)(queryMonth.Where(o => o.CarBrandId == item.Id).Sum(o => o.BYSGCRCTCMB_MonthAacidentCarToFactoryTarget) * 时间进度), 0, MidpointRounding.AwayFromZero),

                        实际台次 = queryDay.Where(o => o.CarBrandId == item.Id).Sum(o => o.SGRCTC_AccidentCarToFactory)
                    });
                }
                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "AccidentCars_RCTC";
                model.BarModel.Controller = "Accident";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                t.Commit();
            }
            return View(model);
        }

        public ActionResult AccidentCar_RCTCKPI(int brandid = 0)
        {
            if (brandid == 0)
            {
                brandid = SearchCarBrandId ?? 0;
            }

            AfterSaleHealthyCarBrand_RCTCModel model = new AfterSaleHealthyCarBrand_RCTCModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(AfterSaleCarKPIService);

                model.MonthData = AfterSaleCarKPIService.GetAfterSaleCarTypeMonthConfigs(Month.Year, Month.Month, Store.Id).Where(o => o.IsConfirmed && o.CarBrandId == brandid).FirstOrDefault();

                model.DayDatas = AfterSaleCarKPIService.GetAfterSaleCarTypeMonthFinished(Month.Year, Month.Month, Store.Id).Where(o => o.IsConfirmed && o.CarBrandId == brandid).ToList();

                ViewBag.时间进度 = 时间进度;
                model.TimeRate = new Models.Chart.Pie();
                model.TimeRate.Width = 350;
                model.TimeRate.Height = 300;
                model.TimeRate.IsRate = true;
                model.TimeRate.YAxis = "%";
                model.TimeRate.Name = "时间进度";
                model.TimeRate.Data.Add("时间进度", 时间进度);
                model.TimeRate.Data.Add("剩余", 1 - 时间进度);

                this.InjectReportModel(model);
                model.BarModel.Action = "AccidentCar_RCTCKPI";
                model.BarModel.Controller = "Accident";
				if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
                model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt() ,referdesc="售后管理>事故维修动态>车型查询>事故车入厂台次完成进度差" }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a> <a class='historyrecord edit'  backtype='html' href='" + Url.Action("AccidentCar_RCTCKPIHistory", new { brandid = brandid }) + "' target='navtab'><span >历史查询</span></a>";
		}else
		{
			 model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AccidentCar_RCTCKPIHistory", new { brandid = brandid }) + "' target='navtab'><span >历史查询</span></a>";
			}
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                
                //model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
                model.BarModel.SearchCarBrandId = brandid;
                t.Commit();
            }
            return View(model);
        }

        public ActionResult AccidentCar_RCTCKPIHistory(int brandid = 0)
        {
            AfterSaleCarModelHistoryModel model = new AfterSaleCarModelHistoryModel();

            if (brandid == 0)
                brandid = SearchCarBrandId ?? 0;
            model.BrandId = brandid;

            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, End.Value.LastOrNowDay());
            }

            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);

                model.CTDays = AfterSaleCarKPIService.GetAfterSaleCarKpiDayDataQuery().Where(o => o.IsConfirmed && o.AddTime <= end && o.AddTime >= start && o.CarBrandId == brandid && o.StoreId == Store.Id).ToList();

                model.CTMonths = AfterSaleCarKPIService.GetAfterSaleCarKpiMonthDataQuery().Where(o => o.IsConfirmed && o.Month <= end && o.Month >= start && o.CarBrandId == brandid && o.StoreId == Store.Id).ToList();

            }

            this.InjectReportModel(model);
            model.BarModel.Action = "AccidentCar_RCTCKPIHistory";
            model.BarModel.Controller = "Accident";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.Start = start;
            model.BarModel.End = end;
            model.BarModel.SearchCarBrandId = brandid;

            return View(model);

        }

        public ActionResult AccidentCars_CZ()
        {
            AfterSaleCarModelHistoryModel model = new AfterSaleCarModelHistoryModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());

                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);

                int[] SAemploys = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.ServiceConsultant).Select(o => o.Id).ToArray();

                model.CTDays = AfterSaleCarKPIService.GetAfterSaleCarKpiDayDataQuery().Where(o => o.IsConfirmed && o.AddTime.Year==Month.Year && o.AddTime.Month==Month.Month && o.StoreId == Store.Id).ToList();

                model.CTMonths = AfterSaleCarKPIService.GetAfterSaleCarKpiMonthDataQuery().Where(o => o.IsConfirmed && o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.StoreId == Store.Id).ToList();

                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "AccidentCars_CZ";
                model.BarModel.Controller = "Accident";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                t.Commit();

            }
            return View(model);
        }

        public ActionResult AccidentCar_CZKPI(int brandid = 0)
        {
            if (brandid == 0)
            {
                brandid = SearchCarBrandId ?? 0;
            }

            AfterSaleCarModelHistoryModel model = new AfterSaleCarModelHistoryModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());

                t.Inject(AfterSaleCarKPIService);

                model.CTDays = AfterSaleCarKPIService.GetAfterSaleCarKpiDayDataQuery().Where(o => o.IsConfirmed && o.AddTime.Year == Month.Year && o.AddTime.Month == Month.Month && o.StoreId == Store.Id && o.CarBrandId==brandid).ToList();

                model.CTMonths = AfterSaleCarKPIService.GetAfterSaleCarKpiMonthDataQuery().Where(o => o.IsConfirmed && o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.StoreId == Store.Id && o.CarBrandId == brandid).ToList();

                model.StandardConfig = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfig(Month.Year, Month.Month, Store.Id);

                ViewBag.时间进度 = 时间进度;

                ViewBag.时间进度 = 时间进度;
                model.TimeRate = new Models.Chart.Pie();
                model.TimeRate.Width = 450;
                model.TimeRate.Height = 300;
                model.TimeRate.IsRate = true;
                model.TimeRate.YAxis = "%";
                model.TimeRate.Name = "时间进度";
                model.TimeRate.Data.Add("时间进度", 时间进度);
                model.TimeRate.Data.Add("剩余", 1 - 时间进度);

                this.InjectReportModel(model);
                model.BarModel.Action = "AccidentCar_CZKPI";
                model.BarModel.Controller = "Accident";
				if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
                model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt() ,referdesc="售后管理>事故维修动态>车型查询>事故车产值完成进度差" }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a> <a class='historyrecord edit'  backtype='html' href='" + Url.Action("AccidentCar_CZHistory", new { brandid = brandid }) + "' target='navtab'><span >历史查询</span></a>";
			}else
		{
			 model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AccidentCar_CZHistory", new { brandid = brandid }) + "' target='navtab'><span >历史查询</span></a>";
			}
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);

                
                model.BarModel.SearchCarBrandId = brandid;
                t.Commit();
            }
            return View(model);
        }

        public ActionResult AccidentCar_CZHistory(int brandid = 0)
        {
            AfterSaleCarModelHistoryModel model = new AfterSaleCarModelHistoryModel();

            if (brandid == 0)
                brandid = SearchCarBrandId ?? 0;
            model.BrandId = brandid;

            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, End.Value.LastOrNowDay());
            }

            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);

                model.CTDays = AfterSaleCarKPIService.GetAfterSaleCarKpiDayDataQuery().Where(o => o.IsConfirmed && o.AddTime <= end && o.AddTime >= start && o.CarBrandId == brandid && o.StoreId == Store.Id).ToList();

                model.CTMonths = AfterSaleCarKPIService.GetAfterSaleCarKpiMonthDataQuery().Where(o => o.IsConfirmed && o.Month <= end && o.Month >= start && o.CarBrandId == brandid && o.StoreId == Store.Id).ToList();

            }

            this.InjectReportModel(model);
            model.BarModel.Action = "AccidentCar_CZHistory";
            model.BarModel.Controller = "Accident";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.Start = start;
            model.BarModel.End = end;
            model.BarModel.SearchCarBrandId = brandid;

            return View(model);
        }


        public ActionResult AccidentCars_SellerIndex()
        {
            AfterSaleHealthyCarTypeModel model = new AfterSaleHealthyCarTypeModel();
            using (var t = new Transtion())
            {

                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());

                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);

                int[] SAemploys = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.ServiceConsultant).Select(o => o.Id).ToArray();

                model.应时台次 = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorMonthTargetsQuery().Where(o => o.AddTime.Year == Month.Year && o.AddTime.Month == Month.Month && o.IsConfirmed && SAemploys.Contains(o.EmployeeId)).Sum(o => o.BYSGCRCTCMB_AccidentCarToFactoryTarget);

                model.实际台次 = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputQuery().ToList().Where(o => o.AddTime.Year == Month.Year && o.AddTime.Month == Month.Month && o.IsConfirmed && SAemploys.Contains(o.EmployeeId)).Sum(o => o.SGRCTC_AccidentToFactory);

                model.实际产值 = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputQuery().ToList().Where(o => o.AddTime.Year == Month.Year && o.AddTime.Month == Month.Month && o.IsConfirmed && SAemploys.Contains(o.EmployeeId)).Sum(o => o.SGCCZ_AccidentCarValue);

                model.应时产值 = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorMonthTargetsQuery().Where(o => o.AddTime.Year == Month.Year && o.AddTime.Month == Month.Month && o.IsConfirmed && SAemploys.Contains(o.EmployeeId)).Sum(o => o.BYSGCCZMB_AccidentCarValueTarget);

                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "AccidentCars_SellerIndex";
                model.BarModel.Controller = "Accident";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                t.Commit();
            }
            return View(model);
        }

        public ActionResult AccidentCars_SellerRCTC()
        {
            AfterSaleReportServiceAdvisorModel model = new AfterSaleReportServiceAdvisorModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());

                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);

                int[] SAemploys = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.ServiceConsultant).Select(o => o.Id).ToArray();


                model.SADayDatas = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputQuery().Where(o => o.IsConfirmed && SAemploys.Contains(o.EmployeeId) && Month.Year == o.AddTime.Year && o.AddTime.Month == Month.Month).ToList();

                model.MonthTarget = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorMonthTargetQuery().Where(o => o.IsConfirmed && SAemploys.Contains(o.EmployeeId) && Month.Year == o.AddTime.Year && o.AddTime.Month == Month.Month).ToList();

                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "AccidentCars_SellerRCTC";
                model.BarModel.Controller = "Accident";
                //model.BarModel.SearchCarBrandId = brandid;
                //model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt() }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a> <a class='historyrecord edit'  backtype='html' href='" + Url.Action("BackService_CS", "AfterSellerReport") + "' target='navtab'><span >人员查询</span></a>";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                t.Commit();

            }
            return View(model);
        }

        public ActionResult AccidentCar_SellerRCTCKPI(int sellerid = 0)
        {
            AfterSaleReportServiceAdvisorModel model = new AfterSaleReportServiceAdvisorModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());

                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);

                int[] SAemploys = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.ServiceConsultant).Select(o => o.Id).ToArray();


                model.SADayDatas = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputQuery().Where(o => o.IsConfirmed && SAemploys.Contains(sellerid) && o.EmployeeId == sellerid && Month.Year == o.AddTime.Year && o.AddTime.Month == Month.Month).ToList();
                model.MonthTarget = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorMonthTargetQuery().Where(o => o.IsConfirmed && SAemploys.Contains(sellerid) && o.EmployeeId == sellerid && Month.Year == o.AddTime.Year && o.AddTime.Month == Month.Month).ToList();

                ViewBag.时间进度 = 时间进度;

                model.TimeRate = new Models.Chart.Pie();
                model.TimeRate.Width = 450;
                model.TimeRate.Height = 300;
                model.TimeRate.IsRate = true;
                model.TimeRate.YAxis = "%";
                model.TimeRate.Name = "时间进度";
                model.TimeRate.Data.Add("时间进度", 时间进度);
                model.TimeRate.Data.Add("剩余", 1 - 时间进度);

                this.InjectReportModel(model);
                model.BarModel.Action = "AccidentCar_SellerRCTCKPI";
                model.BarModel.Controller = "AfterSellerReport";
				if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
                model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt()  ,referdesc="售后管理>事故维修动态>人员查询>事故车入厂台次完成进度差"  }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a> <a class='historyrecord edit'  backtype='html' href='" + Url.Action("AccidentCar_SellerRCTCKPIHistory", new { sellerid = sellerid }) + "' target='navtab'><span >历史查询</span></a>";
				}else
		{
			 model.BarModel.PlusItem = " <a class='historyrecord edit'  backtype='html' href='" + Url.Action("AccidentCar_SellerRCTCKPIHistory", new { sellerid = sellerid }) + "' target='navtab'><span >历史查询</span></a>";
			}
                model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                model.BarModel.SearchSellerId = sellerid;
                t.Commit();

            }
            return View(model);
        }

        public ActionResult AccidentCar_SellerRCTCKPIHistory(int sellerid = 0)
        {
            if (sellerid == 0)
                sellerid = SearchSellerId ?? 0;

            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, End.Value.LastOrNowDay());
            }

            AfterSaleReportServiceAdvisorModel model = new AfterSaleReportServiceAdvisorModel();
            using (var t = new Transtion())
            {

                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);

                int[] SAemploys = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.ServiceConsultant).Select(o => o.Id).ToArray();

                model.SADayDatas = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputQuery().Where(o => o.IsConfirmed && SAemploys.Contains(sellerid) && o.EmployeeId == sellerid && Month <= end && o.AddTime >= start).ToList();
                model.MonthTarget = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorMonthTargetQuery().Where(o => o.IsConfirmed && SAemploys.Contains(sellerid) && o.EmployeeId == sellerid && Month <= end && o.AddTime >= start).ToList();


                this.InjectReportModel(model);
                model.BarModel.Action = "Seller_RCTCCYXXXHistory";
                model.BarModel.Controller = "AfterSellerReport";
                model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
                model.BarModel.Start = start;
                model.BarModel.End = end;
                model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
                model.BarModel.SearchSellerId = sellerid;
                t.Commit();
            }
            return View(model);
        }

        public ActionResult AccidentCars_SellerCZ()
        {
            AfterSaleReportServiceAdvisorModel model = new AfterSaleReportServiceAdvisorModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);

                int[] SAemploys = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.ServiceConsultant).Select(o => o.Id).ToArray();

                model.SADayDatas = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputQuery().Where(o => o.IsConfirmed && SAemploys.Contains(o.EmployeeId) && Month.Year == o.AddTime.Year && o.AddTime.Month == Month.Month).ToList();
                model.MonthTarget = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorMonthTargetQuery().Where(o => o.IsConfirmed && SAemploys.Contains(o.EmployeeId) && Month.Year == o.AddTime.Year && o.AddTime.Month == Month.Month).ToList();

                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "AccidentCars_SellerCZ";
                model.BarModel.Controller = "Accident";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                t.Commit();
            }
            return View(model);
        }

        public ActionResult AccidentCar_SellerCZKPI(int sellerid = 0)
        {
            if (sellerid == 0)
            {
                sellerid = SearchSellerId ?? 0;
            }

            AfterSaleReportServiceAdvisorModel model = new AfterSaleReportServiceAdvisorModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);

                int[] SAemploys = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.ServiceConsultant).Select(o => o.Id).ToArray();

                model.SADayDatas = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputQuery().Where(o => o.IsConfirmed && SAemploys.Contains(sellerid) && o.EmployeeId == sellerid && Month.Year == o.AddTime.Year && o.AddTime.Month == Month.Month).ToList();
                model.MonthTarget = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorMonthTargetQuery().Where(o => o.IsConfirmed && SAemploys.Contains(sellerid) && o.EmployeeId == sellerid && Month.Year == o.AddTime.Year && o.AddTime.Month == Month.Month).ToList();


                ViewBag.时间进度 = 时间进度;
                model.TimeRate = new Models.Chart.Pie();
                model.TimeRate.Width = 450;
                model.TimeRate.Height = 300;
                model.TimeRate.IsRate = true;
                model.TimeRate.YAxis = "%";
                model.TimeRate.Name = "时间进度";
                model.TimeRate.Data.Add("时间进度", 时间进度);
                model.TimeRate.Data.Add("剩余", 1 - 时间进度);

                this.InjectReportModel(model);
                model.BarModel.Action = "AccidentCar_SellerCZKPI";
                model.BarModel.Controller = "Accident";
				if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
                model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt() ,referdesc="售后管理>事故维修动态>人员查询>事故车产值完成进度差"  }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a> <a class='historyrecord edit'  backtype='html' href='" + Url.Action("AccidentCar_SellerCZHistory", new { brandid = sellerid }) + "' target='navtab'><span >历史查询</span></a>";
				}else
		{
			  model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AccidentCar_SellerCZHistory", new { brandid = sellerid }) + "' target='navtab'><span >历史查询</span></a>";
			}
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);

                //model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
                model.BarModel.SearchCarBrandId = sellerid;
                t.Commit();
            }
            return View(model);
        }

        public ActionResult AccidentCar_SellerCZHistory(int sellerid = 0)
        {
            AfterSaleReportServiceAdvisorModel model = new AfterSaleReportServiceAdvisorModel();

            if (sellerid == 0)
                sellerid = SearchSellerId ?? 0;

            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, End.Value.LastOrNowDay());
            }

            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);
                int[] SAemploys = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.ServiceConsultant).Select(o => o.Id).ToArray();

                model.SADayDatas = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorDayInputQuery().Where(o => o.IsConfirmed && SAemploys.Contains(o.EmployeeId) && o.AddTime <= end && o.AddTime >= start).ToList();
                model.MonthTarget = AfterSaleCarKPIService.GetAfterSaleServiceAdvisorMonthTargetQuery().Where(o => o.IsConfirmed && SAemploys.Contains(o.EmployeeId) && o.AddTime <= end && o.AddTime >= start).ToList();


            }

            this.InjectReportModel(model);
            model.BarModel.Action = "AccidentCar_SellerCZHistory";
            model.BarModel.Controller = "Accident";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.Start = start;
            model.BarModel.End = end;
            model.BarModel.SearchSellerId = sellerid;

            return View(model);
        }
    }
}
